package com.zhongyewx.teachercert.views;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.RelativeLayout;

import com.zhongyewx.teachercert.R;

/**
 * @author h.f  类似雷达扫描功能
 * @version  2019-7-24
 */
public class CircleWaveDivergenceView extends RelativeLayout {

    private Context mContext;
    private Bitmap bitmapOne;
    private Bitmap bitmapTwo;
    private Bitmap bitmapThree;
    private boolean isSearch = true;
    private float offsetArgs = 0;//旋转角度

    public CircleWaveDivergenceView(Context context) {
        super(context);
        this.mContext = context;
        initBitmap();
    }

    public CircleWaveDivergenceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        initBitmap();
    }

    public CircleWaveDivergenceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initBitmap();
    }


    private void initBitmap() {

        if (bitmapOne == null) {
            bitmapOne = Bitmap.createBitmap(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ic_circle_bg));
        }
        if (bitmapTwo == null) {
            bitmapTwo = Bitmap.createBitmap(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ic_circle));
        }
        if (bitmapThree == null) {
            bitmapThree = Bitmap.createBitmap(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ic_pointer));
        }

    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(bitmapOne, getWidth()/2-bitmapOne.getWidth()/2, getHeight()/2-bitmapOne.getHeight()/2, null);

        Rect rect = new Rect(getWidth()/2-bitmapThree.getWidth(), getHeight()/2, getWidth()/2, getHeight()/2+bitmapThree.getHeight());
        canvas.rotate(offsetArgs, getWidth()/2, getHeight()/2);
        canvas.drawBitmap(bitmapThree, null, rect, null);
        if (isSearch) {
            offsetArgs = offsetArgs+3;
        }
//        else {
//            canvas.drawBitmap(bitmapThree, getWidth()/2 - bitmapThree.getWidth(), getHeight()/2, null);
//        }

        canvas.drawBitmap(bitmapTwo, getWidth()/2 - bitmapTwo.getWidth()/2, getHeight()/2 - bitmapTwo.getHeight()/2, null);
        if (isSearch) postInvalidate();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                handleActionDownEvent(event);
                return true;
            case MotionEvent.ACTION_MOVE:

                return true;

            case MotionEvent.ACTION_UP:

                return true;
        }
        return super.onTouchEvent(event);
    }

    public void setSearch(boolean isSearching) {
        this.isSearch = isSearching;
        if (offsetArgs >360) {
            offsetArgs = offsetArgs - ((int) offsetArgs /360) *360;
        }
        postInvalidate();
    }

    private void handleActionDownEvent(MotionEvent event) {
        RectF rectF = new RectF(getWidth()/2 - bitmapTwo.getWidth()/2,
                getHeight()/2-bitmapTwo.getHeight()/2,
                getWidth()/2 + bitmapTwo.getWidth()/2,
                getHeight()/2+bitmapTwo.getHeight()/2);

        if (rectF.contains(event.getX(), event.getY())) {
            if (!isSearch){
                setSearch(true);
            } else {
                setSearch(false);
            }
        }
    }
}
